package gemini.castor.ui.client.page.content.store.shoppingcart.cartsummary;

import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.content.store.shoppingcart.ShoppingCartItem;
import gemini.castor.ui.client.page.widget.NumberFieldStandard;
import gemini.castor.ui.client.page.widget.dialog.GmnDialog;
import gemini.castor.ui.client.page.widget.dialog.GmnDialogEvent;
import gemini.castor.ui.client.page.widget.dialog.GmnDialogHandler;
import gemini.castor.ui.client.utils.HtmlUtils;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.Panel;

public class ShoppingCartSummaryWidget extends Composite implements HasHandlers{

	@UiTemplate("ShoppingCartSummaryWidget.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, ShoppingCartSummaryWidget> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
	private I18nConstants constants;

	public ShoppingCartSummaryWidget() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
		initTable();
		initHeader();
	}
	
	@UiField
	FlexTable ftbSummary;
	
	private void initTable(){
		ColumnFormatter colFmt = ftbSummary.getColumnFormatter();
		
		colFmt.setWidth(0, "10%");
		colFmt.setWidth(1, "40%");
		colFmt.setWidth(2, "10%");
		colFmt.setWidth(3, "10%");
		colFmt.setWidth(4, "10%");
		colFmt.setWidth(5, "10%");
		colFmt.setWidth(6, "10%");
	}
	
	private void initHeader(){
		ftbSummary.setHTML(0, 0, HtmlUtils.bold(constants.htmCode()));
		ftbSummary.setHTML(0, 1, HtmlUtils.bold(constants.htmName()));
		ftbSummary.setHTML(0, 2, HtmlUtils.bold(constants.htmQty()));
		ftbSummary.setHTML(0, 3, HtmlUtils.bold(constants.htmRPrice()));
		ftbSummary.setHTML(0, 4, HtmlUtils.bold(constants.htmPoint()));
		ftbSummary.setHTML(0, 5, HtmlUtils.bold(constants.htmYourPrice()));
		ftbSummary.setHTML(0, 6, HtmlUtils.TAG_SPACE);
		ftbSummary.getRowFormatter().setStyleName(0, "tableHeader");
		ftbSummary.setStyleName("ftbSummary");
	}
	
	private int quantity;
	
	public void loadData(List<ShoppingCartItem> items, boolean isDeleted){
		
		ftbSummary.removeAllRows();
		initHeader();
		
		quantity = 0;

		if(items != null && !items.isEmpty()){
			RowFormatter rowFmt = ftbSummary.getRowFormatter();
			int i = 1;
			for (final ShoppingCartItem it : items) {
				if(!it.isDeleted()){
					if(i%2 == 0){
						rowFmt.setStyleName(i, "rowEven");
					}
					
					quantity += it.getQuantity();
						
					ftbSummary.setHTML(i, 0, it.getProduct().getCode());
					ftbSummary.setHTML(i, 1, it.getProduct().getName());
					
					final NumberFieldStandard tbxQuantity = new NumberFieldStandard();
					tbxQuantity.setValue(String.valueOf(it.getQuantity()));
					
					tbxQuantity.addValueChangeHandler(new ValueChangeHandler<String>() {
						
						@Override
						public void onValueChange(ValueChangeEvent<String> event) {
							it.setQuantity(Integer.parseInt(tbxQuantity.getValue()));
							fireEvent(new ShoppingCartSummaryEvent(it));
						}
					});
					
					ftbSummary.setWidget(i, 2, tbxQuantity);
					
					ftbSummary.setHTML(i, 3, String.valueOf(it.getProduct().getPrices().get(0).getValue()));
					ftbSummary.setHTML(i, 4, String.valueOf(it.getProduct().getVolumePoint()));
					ftbSummary.setHTML(i, 5, String.valueOf(it.getProduct().getPrices().get(0).getValue()));
					
					if (isDeleted) {
						HTML htmRemove = new HTML("<div id=\"btnDelete\">"
								+ HtmlUtils.TAG_SPACE + "</div>");
						htmRemove.addClickHandler(new ClickHandler() {

							@Override
							public void onClick(ClickEvent event) {
								confirmDelete(it);
							}
						});

						ftbSummary.setWidget(i, 6, htmRemove);
					} else {
						final CheckBox chxSelect = new CheckBox();
						chxSelect.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
							
							@Override
							public void onValueChange(ValueChangeEvent<Boolean> event) {
								it.setSelected(chxSelect.getValue());
								if(it.getQuantity() <= 0){
									it.setQuantity(1);
									tbxQuantity.setValue(String.valueOf(it.getQuantity()));
									fireEvent(new ShoppingCartSummaryEvent(it));
								}
							}
						});
						ftbSummary.setWidget(i, 6, chxSelect);
					}
					
					i++;
				}
			}
		}		
	}
	
	private void confirmDelete(final ShoppingCartItem it){
		String message = constants.htmAreYouSure();

		GmnDialog dlgConfirm = new GmnDialog("Confirm", message, "Yes", "No");

		dlgConfirm.addConfirmationHandler(new GmnDialogHandler() {

			@Override
			public void onOK(GmnDialogEvent event) {
				it.setDeleted(true);
				fireEvent(new ShoppingCartSummaryEvent(it));
			}

			@Override
			public void onCancel(GmnDialogEvent event) {
				
			}
		});

		dlgConfirm.show();
	}
	
	public HandlerRegistration addItemsChangedHandler(ShoppingCartSummaryHandler handler) {
		return addHandler(handler,  ShoppingCartSummaryEvent.getType());
	}
}
